var Workport = Workport || {};
//@Path("/Profile")
Workport.UserAPI = (function(){
	var root = WP_RESTFUL_URL + (WP_RESTFUL_URL.endsWith('/') ? '' : '/') + 'Profile/';
	
	var urls = ["", "Basic", "{1}", "Image", "Access", "{1}/Follow", "Network", "Auth"];
	
	function getUrl(/*index, replace1, replace2, ...*/) {
		var index, i, loops, r;
		loops = arguments.length;
		if (loops > 0) {
			index = arguments[0];
			r = root + urls[index];
			for (i = 1; i < loops; i++)
				r = r.replace("{"+i+"}", arguments[i]);
		}
		return r;
	}
	
	function sendRequest(method,url,callback,query,sync,data){
		if(method == 'get'){
			if(sync){
				return sfw.Http.syncGet(url,callback,query);
			} else {
				return sfw.Http.get(url,callback,query);
			}
		} else if(method == 'post'){
			if(sync){
				return sfw.Http.syncPost(url,callback,query,data);
			} else {
				return sfw.Http.post(url,callback,query,data);
			}
		} else if(method == 'put'){
			if(sync){
				return sfw.Http.syncPut(url,callback,query,data);
			} else {
				return sfw.Http.put(url,callback,query,data);
			}
		} 
	}
	
	return {
		// @GET	@Path("/")
		// public UserDetailResponse UserDetails(
		getUserDetail:function(handler,sync){
			var url = "Profile";
			return sendRequest('get',getUrl(0),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data,xhr);
					}
				}
			},null,sync);
			function toViewModel(data){
				var view = {};
				if(data.UserBasicDetailsModel){
					view.code = data.ResponseCode;
					view.message = data.ResponseMessage;
					view.userImage = data.UserBasicDetailsModel[0].UserImageURL;
					view.username = data.UserBasicDetailsModel[0].Name;
					view.jobTitle = data.UserBasicDetailsModel[0].Department;
					view.nickname = data.UserBasicDetailsModel[0].NickName;
					view.userId = data.UserBasicDetailsModel[0].AccountName;
					view.mailingAddress = data.UserBasicDetailsModel[0].Office;
					view.emailBusiness = data.UserBasicDetailsModel[0].WorkEmail;
					//view.emailPersonal = data.UserBasicDetailsModel[0].;
					view.phoneBusiness = data.UserBasicDetailsModel[0].Workphone;
					view.phoneMobile = data.UserBasicDetailsModel[0].Mobilephone;
					//view.phoneHome = data.UserBasicDetailsModel[0].;
					//view.leaveInfo = data.UserBasicDetailsModel[0].;
					view.tags = data.UserBasicDetailsModel[0].Tags;
					//view.website = data.UserBasicDetailsModel[0].BlogURL;
					//view.resumeCV = data.UserBasicDetailsModel[0].;
					//view.socialNetworks = data.UserBasicDetailsModel[0].;
					view.desc = data.UserBasicDetailsModel[0].Aboutme;
					view.notes = data.UserBasicDetailsModel[0].CurrentStatus;
				}
				return view;
			}
		},
		// @GET	@Path("/Basic")
		// public UserDetailResponse UserDetails(
		getUserBasic:function(handler){
			return sfw.Http.get(getUrl(1),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data, xhr);
					}
				}
			});
			function toViewModel(data){
				var view = {};
				view.code = data.ResponseCode;
				view.message = data.ResponseMessage;
				view.accountName = data.UserBasic[0].AccountName;
				view.name = data.UserBasic[0].Name;
				view.firstName = data.UserBasic[0].FirstName;
				view.lastName = data.UserBasic[0].LastName;
				view.nickName = data.UserBasic[0].NickName;
				view.sipAddress = data.UserBasic[0].SipAddress ;
				return view;
			}
		},
		// @GET	@Path("/{userId}")
		// public UserDetailResponse UserDetails(@PathParam("userId") String userId)
		getVisitUser:function(handler,request){
			return sfw.Http.get(getUrl(2,request.userId),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data, xhr);
					}
				}
			});
			function toViewModel(data){
				var view = {};
				view.code = data.ResponseCode;
				view.message = data.ResponseMessage;
				view.userImage = data.UserBasicDetailsModel[0].UserImageURL;
				view.username = data.UserBasicDetailsModel[0].Name;
				view.jobTitle = data.UserBasicDetailsModel[0].Department;
				view.nickname = data.UserBasicDetailsModel[0].NickName;
				view.userId = data.UserBasicDetailsModel[0].AccountName;
				view.mailingAddress = data.UserBasicDetailsModel[0].Office;
				view.emailBusiness = data.UserBasicDetailsModel[0].WorkEmail;
				//view.emailPersonal = data.UserBasicDetailsModel[0].;
				view.phoneBusiness = data.UserBasicDetailsModel[0].Workphone;
				view.phoneMobile = data.UserBasicDetailsModel[0].Mobilephone;
				//view.phoneHome = data.UserBasicDetailsModel[0].;
				//view.leaveInfo = data.UserBasicDetailsModel[0].;
				view.tags = data.UserBasicDetailsModel[0].Tags;
				//view.website = data.UserBasicDetailsModel[0].BlogURL;
				//view.resumeCV = data.UserBasicDetailsModel[0].;
				//view.socialNetworks = data.UserBasicDetailsModel[0].;
				view.desc = data.UserBasicDetailsModel[0].Aboutme;
				view.notes = data.UserBasicDetailsModel[0].CurrentStatus;
				view.isFollow = data.UserBasicDetailsModel[0].ShowNetwork;
				return view;
			}
		},
		// @PUT	@Path("/")
		// public BasicResponse UpdateUserDetails(UserDetailRequest request)
		updateUserDetail:function(handler,request){
			var domain = {};
			domain.NickName = request.nickName;
			domain.Aboutme = request.about;
			domain.CurrentStatus = request.status;
			domain.Tags = request.tags;
			domain.Skills = request.skill;
			domain.Interests = request.interest;
			return sfw.Http.put(getUrl(0),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data, xhr);
					}
				}
			},{
				NickName : request.nickName,
				Aboutme : request.about,
				CurrentStatus : request.status,
				Tags : request.tags
			});
			function toViewModel(data){
				var view = {};
				view.code = data.ResponseCode;
				view.message = data.ResponseMessage;
				return view;
			}
		},
		// @PUT	@Path("/Image")
		// public BasicResponse UpdateUserDetails(UserDetailRequest request)
		updateUserImage:function(handler,request){
			return sfw.Http.put(getUrl(3),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data, xhr);
					}
				}
			},request.imageBase64);
			function toViewModel(data){
				var view = {};
				view.code = data.ResponseCode;
				view.message = data.ResponseMessage;
				return view;
			}
		},
		// @GET	@Path("/Access")
		// public UserDetailResponse GetUserAccessDetails()
		// getAccessUser:function(handler,sync){
		// 	return sfw.Http.get(getUrl(4),function(xhr,data){
		// 		if (!sfw.Http.handleError(xhr, data)) {
		// 			if(jQuery.isFunction(handler)){
		// 				handler(data, xhr);
		// 			}
		// 		}
		// 	},null,sync);
		// 	function toViewModel(data){
		// 		var view = {};
		// 		view.code = data.ResponseCode;
		// 		view.message = data.ResponseMessage;
		// 		view.users = [];
		// 		if(data.UserAccessDetailsModel){
		// 			for(var i=0;i<data.UserAccessDetailsModel.length;i++){
		// 				view.users[i] = {};
		// 				view.users[i].userId = data.UserAccessDetailsModel[i].UserID;
		// 				if(data.UserAccessDetailsModel[i].ModeratorCommunities){
		// 					view.users[i].moderators = [];
		// 					for(var j=0;j<data.UserAccessDetailsModel[i].ModeratorCommunities.length;j++){
		// 						view.users[i].moderators[j] = {};
		// 						view.users[i].moderators[j].id = data.UserAccessDetailsModel[i].ModeratorCommunities[j].ID;
		// 						view.users[i].moderators[j].name = data.UserAccessDetailsModel[i].ModeratorCommunities[j].Name;
		// 					}
		// 				}
		// 				if(data.UserAccessDetailsModel[i].MemberCommunities){
		// 					view.users[i].members = [];
		// 					for(var j=0;j<data.UserAccessDetailsModel[i].MemberCommunities.length;j++){
		// 						view.users[i].members[j] = {};
		// 						view.users[i].members[j].id = data.UserAccessDetailsModel[i].MemberCommunities[j].ID;
		// 						view.users[i].members[j].name = data.UserAccessDetailsModel[i].MemberCommunities[j].Name;
		// 					}
		// 				}
		// 				if(data.UserAccessDetailsModel[i].ViewerCommunities){
		// 					view.users[i].viewers = [];
		// 					for(var j=0;j<data.UserAccessDetailsModel[i].ViewerCommunities.length;j++){
		// 						view.users[i].viewers[j] = {};
		// 						view.users[i].viewers[j].id = data.UserAccessDetailsModel[i].ViewerCommunities[j].ID;
		// 						view.users[i].viewers[j].name = data.UserAccessDetailsModel[i].ViewerCommunities[j].Name;
		// 					}
		// 				}
		// 			}
		// 		}
		// 		return view;
		// 	}
		// },
		// @PUT	@Path("/{userId}/Follow")
		// public BasicResponse FollowUnfollowUser(String userId)
		followUser:function(handler,request){
			return sfw.Http.put(getUrl(5,request.userId),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data, xhr);
					}
				}
			},false,{
				action : request.action
			});
			function toViewModel(data){
				var view = {};
				view.code = data.ResponseCode;
				view.message = data.ResponseMessage;
				return view;
			}
		},
		// @GET @Path("/NetWork")
		// public NetWorkResponse NetWork()
		// getNetworkUser:function(handler){
		// 	return sfw.Http.get(getUrl(6),function(xhr,data){
		// 		if (!sfw.Http.handleError(xhr, data)) {
		// 			if(jQuery.isFunction(handler)){
		// 				handler(data, xhr);
		// 			}
		// 		}
		// 	});
		// 	function toViewModel(data){
		// 		var view = {};
		// 		view.code = data.ResponseCode;
		// 		view.message = data.ResponseMessage;
		// 		if(data.NetworkUsers){
		// 			view.users = [];
		// 			for(var i=0;i<data.NetworkUsers.length;i++){
		// 				view.users[i] = {};
		// 				view.users[i].userId = data.NetworkUsers[i].UserId;
		// 				view.users[i].userName = data.NetworkUsers[i].UserName;
		// 				view.users[i].fullName = data.NetworkUsers[i].UserFullName;
		// 				view.users[i].emailId = data.NetworkUsers[i].EmailID;
		// 				view.users[i].flag = data.NetworkUsers[i].Flag;
		// 			}
		// 		}
		// 		return view;
		// 	}
		// },
		// @POST @Path("/Auth")
		// public String submit(User user)
		login:function(handler,request,errHandler){
			var domain = {};
			domain.UserID = request.userId;
			domain.Password = request.password;
			return sfw.Http.post(getUrl(7),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data, xhr);
					}
				}
			},{
				UserID : request.userId,
				Password : request.password
			});
			function toViewModel(data){
				var view = {};
				view.token = data.AccessToken;
				view.code = data.ResponseCode;
				view.message = data.ResponseMessage;
				return view;
			}
		}
	}
})();
